package com.agua.enturno.vista.paginas.adm.agendas.horarios;

import com.agua.enturno.modelo.turno.EstadoDeTurno;
import com.agua.enturno.servicios.HorarioService;
import com.agua.enturno.vista.componentes.BootstrapPagingNavigator;
import com.agua.enturno.vista.componentes.ConfirmLink;
import com.agua.enturno.vista.dto.HorarioFullDTO;
import com.agua.enturno.vista.dto.TurnoDTO;
import com.agua.enturno.vista.modelos.DateTimeModel;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;

import java.util.List;

public class TablaAdministracionDeHorarios extends Panel {
	
	private static final long serialVersionUID = 1L;

	private static final int PAGE_SIZE = 10;
	
	@SpringBean
	private HorarioService horarioService;
	private TablaAdministracionDeTurnos admDeTurnosPanel;
	private int horarioSeleccionadoIndex;
	
	private IModel<List<HorarioFullDTO>> model;
	
	public TablaAdministracionDeHorarios(String id, IModel<List<HorarioFullDTO>> model) {
		super(id, model);
		this.model = model;
		this.setOutputMarkupId(true);
		
		PageableListView<HorarioFullDTO> horariosListView = new PageableListView<HorarioFullDTO>("horarios", model, PAGE_SIZE) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<HorarioFullDTO> item) {
				
				HorarioFullDTO horario = item.getModelObject();
				
				AjaxLink<String> horarioLink = getHorarioLink(item.getIndex());
				horarioLink.add(new Label("horario", new DateTimeModel(new Model<DateTime>(horario.getIntervalo().getStart()))));
				item.add(horarioLink);
				
				Label labelHoy = new Label("hoy", "Hoy");
				
				if ( LocalDate.now().toInterval().contains(horario.getIntervalo().getStart()) )
					labelHoy.setVisible(true);
				else
					labelHoy.setVisible(false);
				
				item.add(labelHoy);
				item.add(new Label("vacantes-ocupadas", horario.getVacantesOcupadas().toString()));
				item.add(new Label("vacantes-totales", horario.getVacantesTotales().toString()));
				item.add(new Label("estado", horario.getEstado().toString()));
				
				Link<String> actionLink = getActionLink(horario);
				actionLink.add(getActionLabel(horario));
				item.add(actionLink);
			}
			
		};
		
		add(horariosListView);
		add(new BootstrapPagingNavigator("horarios-navigator", horariosListView));
		
		admDeTurnosPanel = new TablaAdministracionDeTurnos("adm-turnos-panel", new PropertyModel<List<TurnoDTO>>(this, "horarioSeleccionado.turnos"));
		add(admDeTurnosPanel);
	}
		
	private AjaxLink<String> getHorarioLink(final int horarioIndex) {
		return new AjaxLink<String>("link-horario") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				
				target.add(admDeTurnosPanel);
				
				if (horarioSeleccionadoIndex == horarioIndex)
					admDeTurnosPanel.setVisible(!admDeTurnosPanel.isVisible());
				else {
					horarioSeleccionadoIndex = horarioIndex;
					admDeTurnosPanel.setVisible(true);
				}
			}
		};
	}
	
	private Link<String> getActionLink(final HorarioFullDTO horario) {
		
		Link<String> actionLink = null;
		
		if (horario.getEstado().equals(EstadoDeTurno.DESHABILITADO)) {
			
			actionLink = new ConfirmLink("boton-accion", "¿Realmente desea habilitar el horario?") {
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick() {
					horarioService.habilitarHorario(horario.getId());
				}
			};
			actionLink.add(AttributeModifier.replace("class", "btn btn-success"));
		}
		else {
			actionLink = new ConfirmLink("boton-accion", "¿Realmente desea deshabilitar el horario?") {
				private static final long serialVersionUID = 1L;

				@Override
				public void onClick() {
					horarioService.deshabilitarHorario(horario.getId(), "El profesional no podrá asistir");
				}
			};
			actionLink.add(AttributeModifier.replace("class", "btn btn-warning"));
		}
		
		return actionLink;
	}

	private Label getActionLabel(final HorarioFullDTO horario) {
		
		if (horario.getEstado().equals(EstadoDeTurno.DESHABILITADO)) 
			return new Label("texto-accion", "Habilitar");
		else 
			return new Label("texto-accion", "Deshabilitar");
	}
	
	public HorarioFullDTO getHorarioSeleccionado() {
		return model.getObject().get(horarioSeleccionadoIndex);
	}

}
